﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;

public partial class HolidayEdit : System.Web.UI.Page
{
    Guid HolidayID = Guid.Empty;
    HolidaysManager manager = new HolidaysManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Guid.TryParse(Request.QueryString["id"], out HolidayID))
        {
            if (!IsPostBack)
            {
                var holiday = manager.Single(s => s.HolidayID == HolidayID);

                txtName.Text = holiday.Name;
                //txtNameGeo.Text = holiday.NameGeo;
                txtHolidayDate.DateValue = holiday.HolidayDate;
                chbType.Checked = holiday.HolidayType;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid)
        {
            return;
        }
        if (HolidayID == Guid.Empty)
        {
            manager.Add(new Holiday
            {
                HolidayID = Guid.NewGuid(),
                Name = txtName.Text,
                //NameGeo = txtNameGeo.Text,
                HolidayType = chbType.Checked,
                //HolidayDate = DateTime.Parse(txtHolidayDate.Text),
                HolidayDate = txtHolidayDate.DateValue.Value,
                IsActive = true,
                CreateDate = DateTime.Now,
                CreatedBy = (Session["User"] as UserHelper).UserID
            });
        }
        else
        {
            var holiday = manager.Single(s => s.HolidayID == HolidayID);

            holiday.Name = txtName.Text;
            //holiday.NameGeo = txtNameGeo.Text;
            holiday.HolidayType = chbType.Checked;
            holiday.HolidayDate = txtHolidayDate.DateValue.Value;
            holiday.LastModifyDate = DateTime.Now;
            holiday.LastModifier = (Session["User"] as UserHelper).UserID;
        }
        manager.SaveChanges();
        Response.Redirect("holidayview.aspx?m=liHoliday");
    }
}